package com.dc.commons.config.profile;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.ObjectUtils;

import com.dc.commons.annotation.profile.Functional;
import com.dc.commons.datasource.DataSourceProvider;
/*******************************************************************************<P>
 * 文件名: FunctionalConfig.java <br>
 * 版本: 1.0<br>
 * 描述: 单元测试环境<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2015年12月17日 上午9:52:24<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 *****************************************************************************
 */

@Configuration
@Functional
@PropertySource(value={"classpath:META-INF/dc/dc-default.properties","classpath:/dc.properties","classpath:/dc-functional.properties"},ignoreResourceNotFound=true)
public class FunctionalConfig implements DataSourceProvider{

	@Value("${jdbc.schema}")
	private String schema;
	
	@Value("${jdbc.scripts}")
	private String[] scripts;
	
	@Bean(name="dataSource")
	@Override
	public DataSource dataSource() throws SQLException {
		
		EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
				      .generateUniqueName(true)
				      .setType(EmbeddedDatabaseType.HSQL)
				     .setScriptEncoding("UTF-8")
				      .ignoreFailedDrops(true);
			if(!ObjectUtils.isEmpty(schema)){
				builder.addScript(schema);
			}
			if(!ObjectUtils.isEmpty(scripts)){
				builder.addScripts(scripts);
			}
				      
			EmbeddedDatabase db=builder.build();
		return db;
	}

}
